﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.PortfolioManagement;
using Common.Contract.Customer.Response.PortfolioManagement;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand.PortfolioManagement
{
    public class RetrieveAccountPlanningProductHoldingDataViewByTimeServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveAccountPlanningDataViewRequest, RetrieveAccountPlanningDataViewResponse>
    {
        #region Properties
        public string Cif;
        public int TimeSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveAccountPlanningProductHoldingDataViewByTimeServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveAccountPlanningDataViewRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Cif))
            {
                throw new ArgumentException("Cif customer must be set", "request");
            }
            else
            {
                Cif = Request.Cif;
            }
        }

        protected override bool OnExecute(RetrieveAccountPlanningDataViewResponse response)
        {
            TimeSearch = Request.TimeSearch;
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<AccountPlanningDataViewRecord> records = new List<AccountPlanningDataViewRecord>();
            List<AccountPlanningProductHolding> listCustomer = new List<AccountPlanningProductHolding>();
            listCustomer = Providers.Portfolios.RetrieveAccountPlanningProductHoldingByCif(Cif);
            var listResult = Providers.Portfolios.RetrieveAccountPlanningProductHoldingResultByCif(Cif);
            if (listCustomer != null && listCustomer.Count > 0)
            {
                foreach (var item in listCustomer)
                {
                    int order = item.OrderNum;
                    if (order < 14)
                    {
                        item.GroupCri = "Cho vay";
                    }
                    if (order > 13 && order < 16)
                    {
                        item.GroupCri = "Tiền gửi";
                    }
                    if (order > 15 && order < 23)
                    {
                        item.GroupCri = "Quản lý dòng tiền";
                    }
                    if (order > 23)
                    {
                        item.GroupCri = "Tài trợ thương mại";
                    }
                }

                records = (from ap in listCustomer
                           join r in listResult on new { ap.Cif, ap.Name, ap.OrderNum } equals new { r.Cif, r.Name, r.OrderNum }
                           select new AccountPlanningDataViewRecord
                           {
                               Group = ap.GroupCri,
                               Portfolio = ap.Name,
                               Plan = TimeSearch == 0 ? ap.Year : TimeSearch == 1 ? ap.Month1 : TimeSearch == 2 ? ap.Month2 : TimeSearch == 3 ? ap.Month3 : TimeSearch == 4 ? ap.Month4 : TimeSearch == 5 ? ap.Month5 : TimeSearch == 6 ? ap.Month6 : TimeSearch == 7 ? ap.Month7 : TimeSearch == 8 ? ap.Month8 : TimeSearch == 9 ? ap.Month9 : TimeSearch == 10 ? ap.Month10 : TimeSearch == 11 ? ap.Month11 : ap.Month12,
                               Result = TimeSearch == 0 ? r.Year : TimeSearch == 1 ? r.Month1 : TimeSearch == 2 ? r.Month2 : TimeSearch == 3 ? r.Month3 : TimeSearch == 4 ? r.Month4 : TimeSearch == 5 ? r.Month5 : TimeSearch == 6 ? r.Month6 : TimeSearch == 7 ? r.Month7 : TimeSearch == 8 ? r.Month8 : TimeSearch == 9 ? r.Month9 : TimeSearch == 10 ? r.Month10 : TimeSearch == 11 ? r.Month11 : r.Month12
                           }).ToList();
            }
            response.Records = records;
            return isSuccess;
        }
    }
}
